package cn.sgjk.train.member.controller;


import cn.sgjk.train.common.resp.CommonResp;
import cn.sgjk.train.member.config.MemberApplication;
import cn.sgjk.train.member.req.MemberLoginReq;
import cn.sgjk.train.member.req.MemberRegisterReq;
import cn.sgjk.train.member.req.MemberSendCodeReq;
import cn.sgjk.train.member.resp.MemberLoginResp;
import cn.sgjk.train.member.service.MemberService;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member")
public class MemberController {
    private static final Logger LOG = LoggerFactory.getLogger(MemberController.class);
    @Autowired
    private MemberService memberService;

    @GetMapping("/count")
    public CommonResp<Integer> count() {
        Integer result = memberService.count();
        return new CommonResp<>(result);
    }

    @PostMapping("/register")
    public CommonResp<Long> register(@Valid MemberRegisterReq memberReq) {
        long register = memberService.register(memberReq.getMobile());
        return new CommonResp<>(register);
    }

    @PostMapping("/sendCode")
    public CommonResp<String> sendCode(@Valid @RequestBody MemberSendCodeReq memberReq) {
        LOG.debug("memberReq：{}",memberReq);
        memberService.sendCode(memberReq);
        return new CommonResp<>("发送验证码成功");
    }

    @PostMapping("/login")
    public CommonResp<MemberLoginResp> login(@Valid @RequestBody MemberLoginReq loginReq) {
        MemberLoginResp loginResp = memberService.login(loginReq);
        return new CommonResp<>(loginResp);
    }


}

